package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author Aimer
 * @since 2025-11-14
 */
// @Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final RabbitMqHelper rabbitMqHelper;
    @Override
    public void addLikeRecord(LikeRecordFormDTO likeRecordFormDTO) {
        // 判断是新增点赞还是取消点赞
        boolean success = likeRecordFormDTO.getLiked() ? like(likeRecordFormDTO) : unlike(likeRecordFormDTO);
        // 判断是否成功失败直接结束
        if (!success){
            return;
        }
        // 统计点赞的这个业务点赞总数
        int count = count(new LambdaQueryWrapper<LikedRecord>().eq(LikedRecord::getBizId, likeRecordFormDTO.getBizId()));
        // 发送mq通知
        rabbitMqHelper.send(
                LIKE_RECORD_EXCHANGE,
                StringUtils.format(LIKED_TIMES_KEY_TEMPLATE, likeRecordFormDTO.getBizType()),
                LikedTimesDTO.of(likeRecordFormDTO.getBizId(), count));
    }

    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {
        Long userId = UserContext.getUser();
        List<LikedRecord> records = lambdaQuery().in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, userId)
                .list();
        if (CollUtils.isEmpty(records)){
            return Collections.emptySet();
        }
        return records.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }

    @Override
    public void readLikedTimesAndSendMessage(String bizType,Integer mas) {

    }

    private boolean unlike(LikeRecordFormDTO likeRecordFormDTO) {
        return remove(new LambdaQueryWrapper<LikedRecord>().eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId,likeRecordFormDTO.getBizId()));
    }

    private boolean like(LikeRecordFormDTO likeRecordFormDTO) {
        // 判断当前用户是否已经点过赞
        LambdaQueryWrapper<LikedRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LikedRecord::getUserId, UserContext.getUser());
        lambdaQueryWrapper.eq(LikedRecord::getBizId,likeRecordFormDTO.getBizId());
        int count = count(lambdaQueryWrapper);
        if (count > 0){
            // 已经点过赞了
            return false;
        }
        LikedRecord likedRecord = BeanUtils.copyBean(likeRecordFormDTO, LikedRecord.class);
        likedRecord.setUserId(UserContext.getUser());
        return save(likedRecord);
    }
}
